<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-16
 * Time: 上午10:18
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Appstore\Model;


use Think\Model;

class AppstoreCodeTypeModel extends Model{

    public function editData($data)
    {
        if($data['id']){
            $res=$this->save($data);
        }else{
            $data['create_time']=time();
            $res=$this->add($data);
        }
        return $res;
    }

    public function getData($id)
    {
        $data=$this->find($id);
        return $data;
    }

    public function getList($map=array())
    {
        $list=$this->where($map)->select();
        $list=$this->_initSelectData($list);
        return $list;
    }

    /**
     * 初始化查询信息
     * @param array $dataList
     * @return array
     * @author 郑钟良<zzl@ourstu.com>
     */
    private function _initSelectData($dataList = array())
    {
        $versionModel=D('Appstore/AppstoreVersion');
        $goodsModel=D('Appstore/AppstoreGoods');
        $type=array(
            1=>'插件',
            2=>'模块',
            3=>'主题',
            4=>'服务'
        );
        foreach ($dataList as &$val) {
            $val['version_ids']=explode(',',$val['version_ids']);
            foreach($val['version_ids'] as $vl){
                $version=$versionModel->where(array('id'=>$vl))->find();
                $version['goods']=$goodsModel->where(array('id'=>$version['goods_id']))->find();
                $val['versions_list'][]=$version;
                $val['versions'][]=$type[$version['goods']['entity']].'>'.$version['goods']['title'].'【'.$version['title'].'】';
            }
            $val['versions']=implode('；',$val['versions']);
        }
        unset($val);
        return $dataList;
    }

} 